#include "modbuswig.h"
#include "ui_modbuswig.h"

ModbusWig::ModbusWig(QWidget *parent): QWidget(parent), ui(new Ui::ModbusWig)
{
    ui->setupUi(this);
    connect(ui->pushButton_securitycam, SIGNAL(clicked()), this, SLOT(btnClickedsecuritycamSlot()));    // 设置安保人员状态
    connect(ui->pushButton_AC, SIGNAL(clicked()), this, SLOT(btnClickedACSlot()));  // 设置中央空调
    connect(ui->pushButton_oxygensupply, SIGNAL(clicked()), this, SLOT(btnClickedoxygensupplySlot()));  // 设置供氧量
}

ModbusWig::~ModbusWig()
{
    delete ui;
}
/**
 * @brief boolList2 判断是否存在Modbus相关数据
 * @param list
 * @return
 */
int boolList2(QStringList list)
{
    for(int i = 0; i < list.count(); i++)
    {
        if(!list[i].compare("modbus"))
        {
            return 1;
        }
    }
    return 0;
}
/**
 * @brief ModbusWig::dataUpSlot 数据解析槽函数
 * @param Array
 */
void ModbusWig::dataUpSlot(QByteArray Array)
{
    QString payload = Array;
//    qDebug() << "modbuspayload" << payload;
    // 解析json的数据
    QJsonParseError jsonerror;  // 创建一个JSON解析错误对象
    QJsonDocument doc = QJsonDocument::fromJson(payload.toLatin1(), &jsonerror); // 将JSON数据解析为QJsonDocument对象
    if(!doc.isNull() && jsonerror.error == QJsonParseError::NoError)
    {
        if(doc.isObject())  // 如果是一个对象
        {
            QJsonObject objectroot = doc.object();  // 获取根节点
            qDebug() << "modbus根节点：" << objectroot;
            QStringList listkeys = objectroot.keys();// 获取JSON对象的所有键
            qDebug() << "modbus所有键：" << listkeys;
            int a = boolList2(listkeys);
            qDebug() << "modbus exists" << a;
            if(boolList2(listkeys)) // 变化上报
            {
                qDebug() << "modbus变化上报";
                QJsonObject modbusobj = objectroot.value("modbus").toObject();
                QJsonArray modbusDataArray = modbusobj.value("data").toArray();
                // 使用常量迭代器遍历 QJsonArray
                for (QJsonArray::const_iterator it = modbusDataArray.begin(); it != modbusDataArray.end(); ++it)
                {
                    QJsonObject itemObj = it->toObject();
                    int key = itemObj.value("key").toInt();
                    //QString name = itemObj.value("name").toString();
                    switch(key)
                    {
                        case 101:
                            {
                                int val = itemObj.value("val").toInt();
                                ui->lineEdit_onduty->setText(QString::number(val));
                                break;
                            }
                        case 102:
                            {
                                int val = itemObj.value("val").toInt();
                                ui->lineEdit_patients->setText(QString::number(val));
                                break;
                            }
                        case 103:
                            {
                                double val = itemObj.value("val").toDouble();

                                ui->lineEdit_CO2->setText(QString::number(val));
                                break;
                            }
                        case 104:
                            {
                                bool val = itemObj.value("val").toBool();
                                if(val == true)
                                {
                                    ui->pushButton_securitycam->setText("ON");
                                }
                                else
                                {
                                    ui->pushButton_securitycam->setText("OFF");
                                }
                                break;
                            }
                        case 105:
                            {
                                bool val = itemObj.value("val").toBool();
                                if(val == true)
                                {
                                    ui->pushButton_AC->setText("ON");
                                }
                                else
                                {
                                    ui->pushButton_AC->setText("OFF");
                                }
                                break;
                            }
                        case 106:
                            {
                                bool val = itemObj.value("val").toBool();
                                if(val == true)
                                {
                                    ui->pushButton_oxygensupply->setText("ON");
                                }
                                else
                                {
                                    ui->pushButton_oxygensupply->setText("OFF");
                                }
                                break;
                            }
                        default:
                            break;

                    }
                    qDebug() << "modbus:" << key;
                }
            }
            else    // 设置值
            {
                qDebug() << "modbus设置值";
                int key = objectroot.value("key").toInt();
                switch(key)
                {
                    case 101:
                        {
                            int val = objectroot.value("val").toInt();
                            ui->lineEdit_onduty->setText(QString::number(val));
                            break;
                        }
                    case 102:
                        {
                            int val = objectroot.value("val").toInt();

                            ui->lineEdit_patients->setText(QString::number(val));
                            break;
                        }
                    case 103:
                        {
                            double val = objectroot.value("val").toDouble();

                            ui->lineEdit_CO2->setText(QString::number(val));
                            break;
                        }
                    case 104:
                        {
                            bool val = objectroot.value("val").toBool();

                            if(val == true)
                            {
                                ui->pushButton_securitycam->setText("ON");
                            }
                            else
                            {
                                ui->pushButton_securitycam->setText("OFF");
                            }
                            break;
                        }
                    case 105:
                        {
                            bool val = objectroot.value("val").toBool();
                            if(val == true)
                            {
                                ui->pushButton_AC->setText("ON");
                            }
                            else
                            {
                                ui->pushButton_AC->setText("OFF");
                            }
                            break;
                        }
                    case 106:
                        {
                            bool val = objectroot.value("val").toBool();
                            if(val == true)
                            {
                                ui->pushButton_oxygensupply->setText("ON");
                            }
                            else
                            {
                                ui->pushButton_oxygensupply->setText("OFF");
                            }
                            break;
                        }
                    default:
                        break;

                }
            }
        }
    }

}
/**
 * @brief ModbusWig::btnClickedsecuritycamSlot 设置安保系统槽函数
 */
void ModbusWig::btnClickedsecuritycamSlot()
{
    QMQTT::Message message;
    // 设置主题
    message.setTopic("down");
    static int flag = 0;
    QByteArray data;
    if(flag == 0)
    {
        data = " { \"pattern\" :1, \"key\":104,\"val\":0,\"devtype\":2} ";
        flag = 1;
        ui->pushButton_securitycam->setText("OFF");
    }
    else if(flag == 1)
    {
        data = " { \"pattern\" :1, \"key\":104,\"val\":1,\"devtype\":2} ";
        flag = 0;
        ui->pushButton_securitycam->setText("ON");

    }
    qDebug() << "flag" << flag;
    message.setPayload(data);
    // 发送命令
    if (GlobalVal::mqtt)
    {
        GlobalVal::mqtt->publish(message);
    }
    else
    {
        qDebug() << "MQTT client is not initialized.";
    }
}
/**
 * @brief ModbusWig::btnClickedACSlot
 */
void ModbusWig::btnClickedACSlot()
{
    QMQTT::Message message;
    // 设置主题
    message.setTopic("down");
    static int flag = 0;
    QByteArray data;
    if(flag == 0)
    {
        data = " { \"pattern\" :1, \"key\":105,\"val\":0,\"devtype\":2} ";
        flag = 1;
        ui->pushButton_AC->setText("OFF");
    }
    else if(flag == 1)
    {
        data = " { \"pattern\" :1, \"key\":105,\"val\":1,\"devtype\":2} ";
        flag = 0;
        ui->pushButton_AC->setText("ON");
    }
    message.setPayload(data);
    // 发送命令
    if (GlobalVal::mqtt)
    {
        GlobalVal::mqtt->publish(message);
    }
    else
    {
        qDebug() << "MQTT client is not initialized.";
    }
}
/**
 * @brief ModbusWig::btnClickedoxygensupplySlot 设置供氧系统槽函数
 */
void ModbusWig::btnClickedoxygensupplySlot()
{
    QMQTT::Message message;
    // 设置主题
    message.setTopic("down");
    static int flag = 0;
    QByteArray data;
    if(flag == 0)
    {
        data = " { \"pattern\" :1, \"key\":106,\"val\":0,\"devtype\":2} ";
        flag = 1;
        ui->pushButton_oxygensupply->setText("OFF");
    }
    else if(flag == 1)
    {
        data = " { \"pattern\" :1, \"key\":106,\"val\":1,\"devtype\":2} ";
        flag = 0;
        ui->pushButton_oxygensupply->setText("ON");
    }
    message.setPayload(data);
    // 发送命令
    if (GlobalVal::mqtt)
    {
        GlobalVal::mqtt->publish(message);
    }
    else
    {
        qDebug() << "MQTT client is not initialized.";
    }
}
